package uiLayer;

import uiLayer.util.EditViewTemplate;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import controlLayer.ClassCtr;
import controlLayer.TeacherCtr;
import modelLayer.Teacher;
import modelLayer.Class;

public class ClassEditPanel extends EditViewTemplate {
	private static final long serialVersionUID = 1L;
	
	private ClassPanel root;
	private ClassCtr classCtr;
	private Class cl;
	private JTextField txtName;
	private JTextField txtGrade;
	private JTextField txtYearOf;
	private JComboBox<Teacher> cBoxClTeacher;
	private List<Teacher> teacherList;
	private DefaultComboBoxModel<Teacher> cBoxTeacherList;
	
	public ClassEditPanel(ClassPanel root, ClassCtr classCtr, Class cl){
		
		this.root = root;
		this.classCtr = classCtr;
		this.cl = cl;
		JPanel centerPanel = new JPanel(new BorderLayout());		
		super.getCenterPanel().add(centerPanel, BorderLayout.NORTH);
		super.getBtnReset().setText("Nulstil");
		
		JPanel textInputPanel = new JPanel();
		textInputPanel.setBorder(new EmptyBorder(20, 60, 20, 60));
		centerPanel.add(textInputPanel, BorderLayout.CENTER);
		textInputPanel.setLayout(new GridLayout(4, 2, 0, 10));
		
		JLabel lblName = new JLabel("Navn:");
		textInputPanel.add(lblName);
		
		txtName = new JTextField();
		textInputPanel.add(txtName);
		txtName.setColumns(10);
		
		JLabel lblGrade = new JLabel("Klassetrin:");
		textInputPanel.add(lblGrade);
		
		txtGrade = new JTextField();
		textInputPanel.add(txtGrade);
		txtGrade.setColumns(10);
		
		JLabel lblYearOf = new JLabel("\u00C5rgang:");
		textInputPanel.add(lblYearOf);
		
		txtYearOf = new JTextField();
		textInputPanel.add(txtYearOf);
		txtYearOf.setColumns(10);
		
		JLabel lblClTeacher = new JLabel("Klassel\u00E6rer:");
		textInputPanel.add(lblClTeacher);
		
		cBoxClTeacher = new JComboBox<Teacher>();
		cBoxTeacherList = new DefaultComboBoxModel<Teacher>();
		initiateCBoxTeacher();
		cBoxClTeacher.setModel(cBoxTeacherList);
		textInputPanel.add(cBoxClTeacher);
		
		if (cl != null ){
			txtName.setText(cl.getClassName());
			txtGrade.setText(cl.getGrade());
			txtYearOf.setText(cl.getYearOf());
			Teacher t = cl.getClassTeacher();
			int i = teacherList.indexOf(t);
			cBoxClTeacher.setSelectedIndex(i);
		}
	}

	private void initiateCBoxTeacher() {
		teacherList = null;
		try {
			teacherList = new TeacherCtr().getAll();
			if (teacherList != null){
				for (Teacher t : teacherList) {
					cBoxTeacherList.addElement(t);
				}
			}
			else {
				new StandardDialog().objectNotFoundDialog("Lærer ");
			}
		}
		catch (Exception e) {
			//new StandardDialog().showErrorMessage("Check database connection");
		}
		
	}

	@Override
	protected void doReset() {
		txtName.setText("");
		txtGrade.setText("");
		txtYearOf.setText("");
	}

	@Override
	protected void doSave() {
		String name = txtName.getText();
		String grade = txtGrade.getText();
		String yearOf = txtYearOf.getText();
		Teacher ct = (Teacher) cBoxClTeacher.getSelectedItem();

		if(this.cl == null) {
			try {
				classCtr.createClass(name, grade, yearOf, ct);
				Class newCl = classCtr.findClass(name, true);
				root.getClassListModel().addElement(newCl);
				System.out.println(root.getClassListModel());
				
				new StandardDialog().objectSaved("Klassen");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(this.cl != null) {
			try {
				this.cl.setClassName(name);
				this.cl.setGrade(grade);
				this.cl.setYearOf(yearOf);
				this.cl.setClassTeacher(ct);
				this.classCtr.updateClass(cl);
				new StandardDialog().ObjectUpdated("Klassen");
			}catch (Exception e) {
				
				e.printStackTrace();	
			}
		}
		doReset();
	}
}
